/**
 * @Copyright (c) 2006, 豆窝网
 * @All rights reserved.
 *
 * JS分页类,根据我写的java改写成JS类, 原始处理方法见:DBConn.java文件DBConn:public String page() 
 *
 * @file_name   Page.js
 * @version     1.0
 * @author      黄新泽
 * @date        2007-10-10 16:02:04
 */

/**
 * Class and Function List:
 * Function list:
 * - initialize(obj, line, qs)  
 * - setPageNo(pageNo, start)   
 * - prompt(num, pageNo, start) 
 * - forward()                  
 * - back()                     
 * Classes list:
 * - .Page
 */
var Page = Class.create();
Page.prototype = 
{
	className : 'Page',

	/**
	 * 构造函数
	 *
	 * @param Object	obj			运行对象
	 * @param Int		qs			传递数据
	 * @param Int		line		每页显示记录数
	 *
	 * @access	public
	 */
    initialize: function(obj, line, qs) 
    {
		this.line =  4;		//每页显示的条数
		this.pageLine = 10;		//每页显示的可以连接翻页的条数 << 5 6 7 8 >>

		this.pageNo = 0; //起始页数
		this.start = 0;  //开始偏移量
		this.qs = '';
		this.search = '';

		if (obj)
		{
			this.obj = obj;
			this.obj.func = this.obj.func ? this.obj.func + '.page.execHandle' : this.obj.handle ;
		}
		
		if (line)
		{
			this.line = line;
		}
		
		if (qs)
		{
			this.qs = qs;
		}

		var offset = parseInt(this.pageNo*this.line + this.start);
		if (isNaN(offset))
		{
			offset = 0
		}

		this.fileName = '?' + this.qs + 'offset=' + offset + '&line=' + this.line;

    },


    setPageNo: function(pageNo, start) 
    {
		this.pageNo = pageNo;
		this.start = start;
		return false;

    },

    execHandle: function(pageNo, start) 
    {
		if ('string' == typeof(pageNo))
		{
			if (intVal($(pageNo).value)<1 || intVal($(pageNo).value)>this.total)
			{
				return false;
			}

			start = intVal((intVal($(pageNo).value)-1) / this.pageLine) * this.line *  this.pageLine;
			pageNo = intVal((intVal($(pageNo).value)-1) % (this.pageLine) );
		}

		this.pageNo = pageNo;
		this.start = start;

		eval(this.obj.handle + '(' + pageNo + ', ' + start + ')');
		return false;

    },

	/**
	 *  生成分页
	 *
	 * @param Int		num			总记录数
	 * @param Int		start		开始记录偏移量
	 * @param Array		pageNo		开始页偏移量
	 *
	 * @access	public
	 */
    prompt: function(num, pageNo, start)
    {
		if (num)
		{
			this.num = num;			
		}
		

		if (start)
		{
			this.start = start;				
		}

		if (pageNo)
		{
			this.pageNo = pageNo;
		}

		var offset = parseInt(this.pageNo*this.line + this.start);
		if (isNaN(offset))
		{
			offset = 0
		}

		this.fileName = '?' + this.qs + 'offset=' + offset + '&line=' + this.line;

		/***********************/
        this.pageTotal = this.line * this.pageLine;//所要显示的总条数
        this.pageStart;					//
        this.pageNum;					//
        this.total = parseInt(this.num/this.line) +1 ;	//取得总页数
        this.totalCurrent = parseInt((this.num -this.start+this.line-1) / this.line);	//取得当前总页数
		this.pageCurrent = this.start/this.line + this.pageNo + 1;
        this.totalStart = parseInt((this.num + this.pageTotal - 1) / this.pageTotal);//取得总偏移数
        this.lastStart  = this.num - (this.num % this.pageTotal);//最后一分页第一条记录
        this.returnStr  = '<div class="paging">';


        this.returnStr  +="&nbsp;<a class='total'>" + this.num + "</a><a class='pages'>" + this.pageCurrent + "/" + this.total +  "</a>";
		this.forward();

        this.pageStart = this.start / this.pageTotal * this.pageLine;//取得当前页数
        for(var i=0 ;i<this.totalCurrent&&i<this.pageLine; i++)
        {
            this.pageNum = this.pageStart + i + 1;

			if (this.pageCurrent == this.pageNum)
			{
			    this.returnStr += '<a class="pagenow">' + this.pageNum + '</a>';
			}
			else
			{
			    this.returnStr += ' <a class="page" href="' + this.fileName + '" onclick="return ' + this.obj.func + '(' + i + ', ' + this.start + ', \'' + this.qs + '\');"   title="第' + this.pageNum + '页">' + this.pageNum + '</a>';
			}
            
        }

		this.back();

		var paging_num = 'paging_num' + new	Date().getTime(); 

		this.returnStr += '<a class="input"><input type="text" name="paging_num" id="' + paging_num + '" value="' + this.pageCurrent + '" size="3" style="text-align:center;"> <input type="button" class="btn cp" value="GO" onclick="return ' + this.obj.func + '(\'' + paging_num + '\', ' + this.start + ', \'' + this.qs + '\');"></a></div>';

		return this.returnStr;
    },
	
    forward: function() 
    {
        if(this.start != 0)
        {
			
            var oldStart = this.start - this.pageTotal;

            this.returnStr += '<a  class="around" href="' + this.fileName + '" onclick="return ' + this.obj.func + '(0, 0, \'' + this.qs + '\');"  title="第一页面">|&lsaquo;</a>';
            this.returnStr += '<a class="around" href="' + this.fileName + '"  onclick="return ' + this.obj.func + '(0, ' + oldStart + ', \'' + this.qs + '\');"  title="上一页面">&lsaquo;&lsaquo;</a>';

            //一页一页向上翻
//            if (this.pageNo == 0)
//            {
//                this.returnStr += '&nbsp;&nbsp;<a href="' + this.fileName + '" onclick="return ' + this.obj.func + '(' + (this.pageLine-1) + ', ' + oldStart + ', \'' + this.qs + '\');"  title="上一页"><font face="webdings">7</font></a>';
//            }
//            else
//            {
//				
//                this.returnStr += '&nbsp;&nbsp;<a href="' + this.fileName + '" onclick="return ' + this.obj.func + '(' + (this.pageNo-1) + ', ' + this.start + ', \'' + this.qs + '\'); "  title="上一页"><font face="webdings">7</font></a>';
//            }
        }
    },
	
	back: function() 
    {
        if(this.totalStart != (this.start/this.pageTotal+1) && this.num!=0)
        {
            var newStart = this.start+this.pageTotal;


//            //一页一页向下翻
//            if (this.pageNo == this.pageLine-1)
//            {
//                this.returnStr += '&nbsp;&nbsp;<a href="' + this.fileName + '"  onclick="return '+this.obj.func+'(0, '+newStart+', \''+this.qs+'\');" title="下一页"><font face="webdings">8</font></a>';
//            }
//            else
//            {
//                this.returnStr+='&nbsp;&nbsp;<a href="' + this.fileName + '"onclick="return '+this.obj.func+'('+(this.pageNo+1)+', '+this.start+', \''+this.qs+'\');" title="下一页"><font face="webdings">8</font></a>';
//
//            }

            //一面一面向下翻
            this.returnStr+='<a class="around" href="' + this.fileName + '" onclick="return '+this.obj.func+'(0, '+newStart+', \''+this.qs+'\');"  title="下一页面">&rsaquo;&rsaquo;</a>';

            //最后一面
            this.returnStr+='<a class="around" href="' + this.fileName + '"  onclick="return '+this.obj.func+'(0, '+this.lastStart+', \''+this.qs+'\');"  title="最后一页面">&rsaquo;|</a>';
        }
    }
}


//<script language="JavaScript">
////<![CDATA[

///**
// * 构造函数
// *
// * @param Int		p		起始页数
// * @param Int		s		开始偏移量
// *
// * @access	public
// */
//var loadFronts = {};
//function loadFrontStart(p, s)
//{
//	loadFronts.num = 100;
//	loadFronts.line = 100;
//
//	offset = p * loadFronts.line + s;
//
//	loadFronts.page = function(obj)
//	{
//		return new Page(obj);
//	};
//	
//	$('nav').update(new Page({handle:'loadFrontStart'}).prompt(loadFronts.num, p, s));
//
//	return false;
//}
//
//loadFrontStart(0, 0);

//

//	var logic = {ajaxId:"user_data", act:"findUserImage",  type:"o", cla:"Image", operateType:"class"};
//	var ctl = [];
//
//	var user = new DAjax({url:"../admin/gateway.php", message:"读取用户信息...", page:{func:'user', handle:'getUser'} }, logic, {}, ctl);
//
//
//	user.onComplete   = function()
//	{
//		var s = new Tpl();
//		
//		var t_img = ' {for p in data}	 <div>${p.username|capitalize} ${p.total_chars}</div> {forelse}	 <div>No products in your cart.</div> {/for}';
//
//		//alert(Object.toJSON(this.responseData));
//		$('image_data').update(s.process(t_img, {data:this.responseData}));
//		//$('image_ori').update(Object.toJSON({data:this.responseData}));
//
//		var num = 100;
//
//		$('nav').update(this.page.prompt(num));
//
//	}
//	
//	user.onNoneGet   = function()
//	{
//		ctl.each(Field.clear);
//	}
//
//	function getUser(p, s)
//	{
//
//		//user.page.setPageNo(p, s);
//		//offset = p * user.page.line + s;
//
//		user.queryData['offset'] = p * user.page.line + s;
//		user.queryData['line'] = user.page.line;
//		user.sendRequest();
//
//		return false;
//	}
//
//	getUser(0 , 0)

////]]>>
//</script>

